/* 有获取表格数据的页面的删除数据逻辑
 * dependence - tableCoreMixin， tableBtnLoadingMixin, ConfirmTempMixin
 * 需要提供 - currentDeleteApi - 当前页面的删除接口
-------------------------- */
const tableDeleteMixin = {
  computed: {
    deleteCurrentText () {
      return this.deleteText || '删除'
    },
    deleteCurrentBtnText () {
      return this.deleteBtnText || '删除'
    }
  },
  methods: {
    // 删除单个
    deleteOne (id, index) {
      this.selectIds = id
      this.ConfirmTemp(`确认${this.deleteCurrentText}吗`, () => {
        this.AsyncLoading(this.deleteCurrentBtnText, index, this.deleteApi)
      })
    },
    // 批量删除已选的数据(idKey: 行数据中id对应的字段名)
    deleteSelected (idKey) {
      if (!this.selectList.length) return this.$message.warning('选择数据后才能操作')
      this.selectIds = this.selectList.map(item => item[idKey]).join(',')
      this.ConfirmTemp(`确认${this.deleteCurrentText}选中的${this.selectList.length}条数据吗`, () => {
        this.AsyncLoading(this.deleteCurrentBtnText, this.deleteApi)
      })
    },
    // 删除接口
    async deleteApi () {
      const [ err ] = await this.currentDeleteApi()
      if (err) return this.$message.error(err.message || '操作失败')
      this.$message.success(`${this.deleteCurrentText}成功`)
      this.getTableData()
    }
  }
}

export default tableDeleteMixin
